﻿
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Linq;
using System.Text;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Sunimage.Spjg.Common;
using Sunimage.Spjg.Common.SysMgr;
namespace Sunimage.Spjg.DAL.SysMgr
{
    public class PermissionDa
    {
        /// <summary>
        /// 获取所有权限
        /// </summary>
        /// <returns></returns>
        public static List<Permission> GetAll()
        {
            Database db = DBHelper.GetDatabase();
            List<Permission> perms = new List<Permission>();
            using (IDataReader dataReader = db.ExecuteReader("Usp_GetPermissionList"))
            {
                while (dataReader.Read())
                {
                    perms.Add(PopulatePerm(dataReader));
                }
            }

            return perms;
        }

        //辅助方法
        internal static Permission PopulatePerm(IDataReader dataReader)
        {
            Permission perm = new Permission();
            perm.Id = dataReader.GetInt32(0);
            perm.Name = dataReader.GetString(1);
            perm.Sort = dataReader.GetInt32(2);

            return perm;
        }

        public static List<Permission> GetUserPermissionList(int userId)
        {
            Database db = DBHelper.GetDatabase();
            List<Permission> perms = new List<Permission>();
            using (IDataReader dataReader = db.ExecuteReader("Usp_GetUserPermissions", userId))
            {
                while (dataReader.Read())
                {
                    perms.Add(PopulatePerm(dataReader));
                }
            }

            return perms;
        }
    }
}
